package leetcode.editor.cn;

import java.util.LinkedList;
import java.util.Queue;

public class _297_SerializeAndDeserializeBinaryTree {
    public static void main(String[] args) {
        Codec codec = new _297_SerializeAndDeserializeBinaryTree().new Codec();
        TreeNode a5 = new _297_SerializeAndDeserializeBinaryTree().new TreeNode(5);
        TreeNode a4 = new _297_SerializeAndDeserializeBinaryTree().new TreeNode(4);
        TreeNode a3 = new _297_SerializeAndDeserializeBinaryTree().new TreeNode(3);
        TreeNode a2 = new _297_SerializeAndDeserializeBinaryTree().new TreeNode(2);
        TreeNode a1 = new _297_SerializeAndDeserializeBinaryTree().new TreeNode(1);
        a1.left = a2;
        a1.right = a3;
        a3.left = a4;
        a3.right = a5;
        String serialize = codec.serialize(a1);
        System.out.println(serialize);
        TreeNode deserialize = codec.deserialize(serialize);

    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    //leetcode submit region begin(Prohibit modification and deletion)


    public class Codec {

        // Encodes a tree to a single string.
        public String serialize(TreeNode root) {
            if (root == null) {
                return "null";
            }
            StringBuilder res = new StringBuilder();
            Queue<TreeNode> q = new LinkedList<>();
            q.add(root);
            while (!q.isEmpty()) {
                int sz = q.size();
                for (int i = 0; i < sz; i++) {
                    TreeNode cur = q.peek();
                    q.remove();
                    res.append(",");
                    if (cur != null) {
                        res.append(cur.val);
                        q.add(cur.left);
                        q.add(cur.right);
                    } else {
                        res.append("null");
                    }
                }
            }
            return res.toString().substring(1);
        }

        // Decodes your encoded data to tree.
        public TreeNode deserialize(String data) {
            if ("null".equals(data)) {
                return null;
            }
            String[] nodes = data.split(",");
            TreeNode root = new TreeNode(Integer.valueOf(nodes[0]));
            int idx = 1;
            Queue<TreeNode> q = new LinkedList<>();
            q.add(root);
            while (!q.isEmpty()) {
                TreeNode cur = q.peek();
                q.remove();
                if (nodes[idx].equals("null")) {
                    cur.left = null;
                } else {
                    cur.left = new TreeNode(Integer.valueOf(nodes[idx]));
                    q.add(cur.left);
                }
                idx++;
                if (nodes[idx].equals("null")) {
                    cur.right = null;
                } else {
                    cur.right = new TreeNode(Integer.valueOf(nodes[idx]));
                    q.add(cur.right);
                }
                idx++;
            }
            return root;
        }
    }

// Your Codec object will be instantiated and called as such:
// Codec ser = new Codec();
// Codec deser = new Codec();
// TreeNode ans = deser.deserialize(ser.serialize(root));
//leetcode submit region end(Prohibit modification and deletion)

}